//
//  RegisterViewController.m
//  witMorning
//
//  Created by 信晨汇 on 2018/1/9.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate>

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *usernumber;
@property (nonatomic, copy) NSString *phone;
@property (nonatomic, copy) NSString *password;
@property (nonatomic, copy) NSString *passwordTwo;

@end

@implementation RegisterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, 100, 30, 30)];
    [closeBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:closeBtn];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, SCREEN_W-200, 30)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.text = @"用户注册";
    [self.view addSubview:titleLabel];
    
    NSArray *labelArray = @[@"姓名：",@"工号：",@"登录密码：",@"确认密码：",@"手机号码："];
    NSArray *placeholderArray = @[@"请输入姓名",@"请输入工号",@"请输入密码",@"请确认密码",@""];

    
    for (int i = 0; i<labelArray.count; i++) {
        
        CGRect rect = CGRectMake(10, 150+50*i, SCREEN_W-20, 40);
        UIView *contentView = [self contentViewWithRect:rect name:labelArray[i] placehoder:placeholderArray[i] withIndex:i];
        [self.view addSubview:contentView];
        
    }
    
//    button
    CGFloat y = 150 + 5*50;
    UIButton *phoneBtn = [[UIButton alloc]initWithFrame:CGRectMake(70, y+20, SCREEN_W-140, 45)];
    phoneBtn.tag = 5;
    phoneBtn.backgroundColor = [UIColor blueColor];
    [phoneBtn setTitle:@"点击验证手机号码" forState:UIControlStateNormal];
    [phoneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    phoneBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    phoneBtn.layer.cornerRadius = 5;
    phoneBtn.clipsToBounds = YES;
    [phoneBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:phoneBtn];

    //    button
    UIButton *reg_btn= [[UIButton alloc]initWithFrame:CGRectMake(70, phoneBtn.bottom+10, SCREEN_W-140, 45)];
    reg_btn.tag = 10;
    reg_btn.backgroundColor = [UIColor blueColor];
    [reg_btn setTitle:@"注册" forState:UIControlStateNormal];
    [reg_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    reg_btn.titleLabel.font = [UIFont systemFontOfSize:17];
    reg_btn.layer.cornerRadius = 5;
    reg_btn.clipsToBounds = YES;
    [reg_btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:reg_btn];
  
}


- (UIView *)contentViewWithRect:(CGRect)rect name:(NSString *)title  placehoder:(NSString *)placehoder withIndex:(NSInteger)index
{
    UIView *viewview = [[UIView alloc]initWithFrame:rect];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 80, 30)];
    label.text = title;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentRight;
    [viewview addSubview:label];
    
    UITextField *textf = [[UITextField alloc]initWithFrame:CGRectMake(label.right+10, 15, rect.size.width-80-30, 30)];
    textf.delegate = self;
    textf.tag = (index + 1) * 10;
    textf.placeholder = placehoder;
    textf.font = [UIFont systemFontOfSize:16];
    [viewview addSubview:textf];
    
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(textf.x, textf.bottom, textf.width, 1)];
    lineLabel.backgroundColor = [UIColor lightGrayColor];
    [viewview addSubview:lineLabel];
    

    return viewview;

}




- (void)closeClick:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)buttonClick:(UIButton *)sender
{
    if (sender.tag == 5) {
        //验证手机号
    } else if (sender.tag == 10) {
        if (![self.password isEqualToString:self.passwordTwo]) {
            [MBProgressHUD showMessag:@"两次密码不一致" toView:self.view];
            return;
        }
        //注册
//        NSString *url = [NSString stringWithFormat:@"%@",BaseURL];
//        [[WMManage shareInstance] getRequestWithUrl:url success:^(id responseObj) {
//            
//        } fail:^(id error) {
//            
//        }];
        [[WMManage shareInstance] postRequestWithUrl:[NSString stringWithFormat:@"%@register",BASE_HTTP]
                                            andParam:@{
                                                       @"user.username":self.name?:@"",
                                                       @"user.password":self.password?:@"",
                                                       @"user.usernumber":self.usernumber?:@"",
                                                       @"user.phone":self.phone?:@"",
                                                       }
                                             success:^(id responseObj) {
                                                 [MBProgressHUD showMessag:responseObj[@"result"] toView:self.view];
                                             } fail:^(id error) {
                                                 [MBProgressHUD showMessag:@"注册失败" toView:self.view];
                                             }];
    }
}

#pragma mark -UITextField Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *str = [textField.text stringByAppendingString:string];
    if (textField.tag == 10) {
        self.name = str;
    } else if (textField.tag == 20) {
        self.usernumber = str;
    } else if (textField.tag == 30) {
        self.password = str;
    } else if (textField.tag == 40) {
        self.passwordTwo = str;
    } else if (textField.tag == 50) {
        self.phone = str;
        if (str.length > 11) {
            return NO;
        }
    }
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
